package com.utilsapi;

import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.util.Log;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

/**
 * @anthor liuwenwang
 * QQ:2653256624
 * @time 2017/12/13 11:18
 */
public class Utils {
    private final static String SOFT_VERSION="1.2.3";
    private final static String HARD_VERSION="1.2.0";

    /**
     * 获取设备信息
     * @return
     */
    public static DeviceInfo getDeviceInfo(){
        return new DeviceInfo(getSerialNumber(),getEthernetMac(),SOFT_VERSION,HARD_VERSION,getBuletooth_mac());
    }

    private static String getSerialNumber(){
        String serial = null;
        try {
            Class<?> c =Class.forName("android.os.SystemProperties");
            Method get =c.getMethod("get", String.class);
            serial = (String)get.invoke(c, "ro.serialno");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return serial;
    }
    private static String loadFileAsString(String filePath) throws java.io.IOException{
        StringBuffer fileData = new StringBuffer(1000);
        BufferedReader reader = new BufferedReader(new FileReader(filePath));
        char[] buf = new char[1024];
        int numRead=0;
        while((numRead=reader.read(buf)) != -1){
            String readData = String.valueOf(buf, 0, numRead);
            fileData.append(readData);
        }
        reader.close();
        return fileData.toString();
    }

    private static String getEthernetMac(){
        try {
            return loadFileAsString("/sys/class/net/eth0/address")
                    .toUpperCase().substring(0, 17);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String getEthernetIpAddress() {
        try {
            for (Enumeration<NetworkInterface> en = NetworkInterface
                    .getNetworkInterfaces(); en.hasMoreElements();) {
                NetworkInterface intf = en.nextElement();
                for (Enumeration<InetAddress> enumIpAddr = intf
                        .getInetAddresses(); enumIpAddr.hasMoreElements();) {
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    if (!inetAddress.isLoopbackAddress()) {
                        String ipAddress = inetAddress.getHostAddress().toString();
                        if(!ipAddress.contains("::"))
                            return inetAddress.getHostAddress().toString();
                    }else
                        continue;
                }
            }
        } catch (SocketException ex) {
            Log.e("Utils", ex.toString());
        }
        return "GetHostIP Fail,Please clear the shareReference";
    }

    /**
     * 获取wifi mac需要添加下面权限
     * <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
     */
   private static String getWifiMac(Context context){
       WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
       WifiInfo info = wifi.getConnectionInfo();
       return info.getMacAddress();
   }
    public static String getwifiIp1() {
       try {
           for (Enumeration<NetworkInterface> en = NetworkInterface .getNetworkInterfaces(); en.hasMoreElements();){
               NetworkInterface intf = en.nextElement();
               if (intf.getName().toLowerCase().equals("eth0")) {
                   for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                       InetAddress inetAddress = enumIpAddr.nextElement();
                       if (!inetAddress.isLoopbackAddress()) {
                           String ipaddress = inetAddress.getHostAddress().toString();
                           if(!ipaddress.contains("::")){//ipV6的地址
                                return ipaddress;
                           }
                       }
                   }
               } else {
                   continue;
               }
           }
       } catch (Exception ex) {
           Log.e("Wifi IpAddress", ex.toString());
       }

       return null;
   }
    public static String getwifiIp2(Context context) {
        WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        WifiInfo info = wifi.getConnectionInfo();
        return info.getIpAddress()+"";
    }
   /*
   获取蓝牙地址需要添加下面的权限
   <uses-permission android:name="android.permission.BLUETOOTH" />
   <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    */
private static String getBuletooth_mac(){
    BluetoothAdapter btAda = BluetoothAdapter.getDefaultAdapter();
    //开启蓝牙
    if (btAda.isEnabled() == false) {
        if (btAda.enable()) {
            while (btAda.getState() == BluetoothAdapter.STATE_TURNING_ON
                    || btAda.getState() != BluetoothAdapter.STATE_ON) {
                try {
                    Thread.sleep(100);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
    return btAda.getAddress();
}

}
